import GameManager from "./GameManager";
import TimeTool from "../Tool/TimeTool";
import MsgDispatcher, { MsgType } from "../Tool/MsgDispatcher";

import MathTool from "../Tool/MathTool";



enum CameraState
{
    Start,
    Running,
    GameWaiting,
    Move,
}

export default class CameraMove extends Laya.Script3D {

    private m_playerTransform:Laya.Transform3D;
    private m_disV3:Laya.Vector3=new Laya.Vector3();
    private m_sp:Laya.Sprite3D;
    private m_speed=2;

    public m_CameraState=CameraState.Start;

    public get MySprite3D():Laya.Sprite3D
    {
        if(this.m_sp==null)
        {
            this.m_sp=this.owner as Laya.Sprite3D;
        }
        return  this.m_sp;
    }

    private m_follow:boolean=false;
    

    public xoffset=0;
    public yoffset=110.5;
    public zoffset=-102.1;

    public m_startQua=new Laya.Quaternion(0,0.97,0.22,0);
    public m_startPos=new Laya.Vector3(0,28,-45.8);

    public m_runningQua=new Laya.Quaternion(0,0.927,0.373,0);
    public m_runningPos=new Laya.Vector3(0,110.5,-102.1);
    //public m_runningPos=new Laya.Vector3(0,80.5,-75.1)

    public m_targetStartPos=new Laya.Vector3();
    public m_targetRunningPos=new Laya.Vector3();





    OnTweenTo()
    {   
       this.m_shake=true;
       this.m_shakefactor=1.1;
       this.m_shakevalue=1.5;
    }

    private m_shake=false;
    private m_originalPos=new Laya.Vector3();
    private m_shakevalue=1;
    private m_shakefactor=1.1;
     CameraShake() 
     {
        this. m_originalPos.x=MathTool.GetRandomNum(0, this.m_shakevalue * 2) - this.m_shakevalue;
        this. m_originalPos.y=0;
        this.m_originalPos.z=MathTool.GetRandomNum(0, this.m_shakevalue * 2) - this.m_shakevalue;

        this.m_shakevalue=this.m_shakevalue/this.m_shakefactor;
        if (this.m_shakevalue < 0.01)
        {
            this.m_shake=false;
            this.m_shakevalue = 0;
            this.m_originalPos.x=0;
            this.m_originalPos.z=0;
            this.m_originalPos.y=0;
        }
    }


    public  UpdateFollow(player:Laya.Sprite3D)
    {
        this.m_follow=true;
        this.m_playerTransform=player.transform; 
            

        let ts=this.MySprite3D.transform;
      //  console.log("MySprite3D w="+ ts.rotation.w+" x="+ ts.rotation.x+" y="+ts.rotation.y+" z="+ts.rotation.z);
        Laya.Vector3.add( this.m_playerTransform.position,this.m_startPos,this.m_targetStartPos);
        Laya.Vector3.add( this.m_playerTransform.position,this.m_runningPos,this.m_targetRunningPos);

    }




    private m_multiple=1;

    onUpdate()
    {
        if (this.m_shake) {
            this.CameraShake();
            // return;
        }
        let ts=this.MySprite3D.transform;
        switch (this.m_CameraState) 
        {
            case CameraState.Start:
                let startqua=new Laya.Quaternion();
                Laya.Quaternion.slerp(ts.rotation,this.m_startQua,this.m_speed*TimeTool.DeltaTime,startqua);
               
                ts.rotation=this.m_startQua;
                ts.position=this.m_targetStartPos;    
                this.FollowPos(this.m_targetStartPos);
            break;
            case CameraState.Running:
                let runqua=new Laya.Quaternion();
                Laya.Quaternion.slerp(ts.rotation,this.m_runningQua,this.m_speed*TimeTool.DeltaTime,runqua);
                ts.rotation=runqua;
                this.FollowPos(this.m_targetRunningPos);
            break;
            case CameraState.Move:
                if (this.m_follow) 
                {
                    Laya.Vector3.add( this.m_playerTransform.position, this.m_runningPos,this.m_targetRunningPos);
                    Laya.Vector3.add(this.m_targetRunningPos,this.m_originalPos,this.m_finalPos);
                    ts.position=this.m_finalPos; 
                }
                else
                {
                    Laya.Vector3.add( this.m_playerTransform.position, this.m_runningPos,this.m_targetRunningPos);
                    this.MoveFollowPos(this.m_targetRunningPos);
                }
            break;
        }

    }


    public StopFollow()
    {
        this.m_follow=false;
    }

    private m_finalPos=new Laya.Vector3();
    MoveFollowPos(targetpos:Laya.Vector3)
    {
        let ts=this.MySprite3D.transform;
        let dis=Laya.Vector3.distance(ts.position,targetpos);

        if (dis>0.5)
        {
            let temppos=new Laya.Vector3();
            Laya.Vector3.lerp(ts.position,targetpos,this.m_speed*3*TimeTool.DeltaTime,temppos);
            ts.position=temppos;
        }
        else
        {
            ts.position=targetpos;     
            this.m_follow=true;
        }
    }


    FollowPos(targetpos:Laya.Vector3)
    {
        let ts=this.MySprite3D.transform;
        let dis=Laya.Vector3.distance(ts.position,this.m_startPos);

        if (dis>0.02)
        {
            let temppos=new Laya.Vector3();
            Laya.Vector3.lerp(ts.position,targetpos,this.m_speed*TimeTool.DeltaTime,temppos);
            ts.position=temppos;
        }
        else
        {
            ts.position=targetpos;     
            this.m_follow=true;
        }

    }



    // public OnSkyLoadFinish(cubeTexture) 
    // {
    //     //console.log(GameManager.GetInstance().m_currentScene.skyRenderer);
    //    let skinMat=Laya.loader.getRes(GameDataManager.s_SkyMaterialPath) as Laya.SkyBoxMaterial;
    //    // let skinMat=new Laya.SkyBoxMaterial();
    //     //var skyMat: Laya.SkyBoxMaterial = GameManager.GetInstance().m_currentScene.skyRenderer.material as Laya.SkyBoxMaterial;
    //     skinMat.textureCube = cubeTexture;
    //     GameManager.GetInstance().m_currentScene.skyRenderer.material=skinMat;
    // }

}